package com.ruibang.glass.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.common.domain.file.FileBusiness;
import com.ruibang.glass.quality.domain.req.QualityStripeReq;
import com.ruibang.glass.quality.domain.req.SemiCheckReportReq;
import com.ruibang.glass.quality.domain.resp.SemiCheckDetailData;
import com.ruibang.glass.quality.entity.QualityStress;
import com.ruibang.glass.quality.entity.QualityStripe;
import com.ruibang.glass.quality.feign.FileApi;
import com.ruibang.glass.quality.mapper.QualityStripeMapper;
import com.ruibang.glass.quality.service.QualityStripeService;
import com.ruibang.glass.quality.service.SpecsConfigService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.constant.CommonConstant;
import com.teaming.cloud.framework2.common.model.ResultBody;
import com.teaming.cloud.framework2.common.pojo.PageRequest;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 半成品条纹信息 服务实现类
 * </p>
 *
 * @author wujie
 * @since 2024-03-11
 */
@Service
public class QualityStripeServiceImpl extends ServiceImpl<QualityStripeMapper, QualityStripe> implements QualityStripeService {

    @Autowired
    private FileApi fileApi;

//    @Autowired
//    private RedisSingleService redisService;

    @Autowired
    private RedisSingleService redisService;

    @Autowired
    private SpecsConfigService specsConfigService;

    @Override
    public void saveInfo(QualityStripe qualityStripe) {
        this.save(qualityStripe);
        //更新文件对应业务Id
        if (CollectionUtils.isNotEmpty(qualityStripe.getFileIds())) {
            FileBusiness fileBusiness = new FileBusiness();
            fileBusiness.setFileIds(qualityStripe.getFileIds());
            fileBusiness.setBusinessId(qualityStripe.getStripeId());
            fileApi.updateBusinessById(fileBusiness);
        }
    }

    @Override
    public void updateInfo(QualityStripe qualityStripe) {
        //更新文件信息
        if (CollectionUtils.isNotEmpty(qualityStripe.getDeleteFileIds())) {
            fileApi.deleteByFileIds(qualityStripe.getDeleteFileIds());
        }
        //新增文件
        if (CollectionUtils.isNotEmpty(qualityStripe.getFileIds())) {
            FileBusiness fileBusiness = new FileBusiness();
            fileBusiness.setBusinessId(qualityStripe.getStripeId());
            fileBusiness.setFileIds(qualityStripe.getFileIds());
            fileApi.updateBusinessById(fileBusiness);
        }

        this.updateById(qualityStripe);
    }

    @Override
    public PageResult<QualityStripe> queryPage(PageRequest<QualityStripeReq> pageRequest) {
        final QualityStripeReq param = pageRequest.getParam();
        final LambdaQueryWrapper<QualityStripe> lambda = new QueryWrapper<QualityStripe>().lambda();
        this.buildCondition(lambda, param);
        lambda.orderByDesc(QualityStripe::getTransectTime);
        final IPage<QualityStripe> page = this.page(new Page<>(pageRequest.getPageNo(), pageRequest.getPageSize()), lambda);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<String> ids = page.getRecords().stream().map(c -> c.getStripeId()).collect(Collectors.toList());
            ResultBody<List<FileBusiness>> resultBody = fileApi.getFileBusinessByIds(ids);
            Map<String, List<String>> fileMap = null;
            if (resultBody != null && CollectionUtils.isNotEmpty(resultBody.getData())) {
                fileMap = resultBody.getData().stream().collect(Collectors.toMap(f -> f.getBusinessId(), f -> f.getFileIds(), (f1, f2) -> f1));
            }

            Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);

            for (QualityStripe record : page.getRecords()) {
                if (fileMap != null) {
                    record.setFileIds(fileMap.get(record.getStripeId()));
                }

                if (userMap != null) {
                    record.setUserName(userMap.get(record.getUserId()) == null ? "" : userMap.get(record.getUserId()).toString());
                }
            }
        }

        PageResult<QualityStripe> pr = new PageResult(page);
        return pr;
    }

    @Override
    public List<QualityStripe> queryQualityStripe(QualityStripeReq stripeReq) {
        final LambdaQueryWrapper<QualityStripe> lambda = new QueryWrapper<QualityStripe>().lambda();
        this.buildCondition(lambda, stripeReq);
        lambda.orderByAsc(QualityStripe::getTransectTime);
        return this.list(lambda);
    }

    @Override
    public List<SemiCheckDetailData> queryStripeCheckData(SemiCheckReportReq reportReq) {
        return baseMapper.queryStripeCheckData(reportReq);
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(QualityStripe::getStripeId, id).set(QualityStripe::getIsDelete, CommonConstant.ONE_STR).update();
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<QualityStripe> lambda, QualityStripeReq param) {
        lambda.eq(QualityStripe::getIsDelete, CommonConstant.ZERO_STR);
        if (null == param) return;
        if (StringUtils.isNotBlank(param.getGlassId())) {
            lambda.like(QualityStripe::getGlassId, param.getGlassId());
        }

        if (param.getThickStripeStart() != null && param.getThickStripeEnd() != null) {
            lambda.ge(QualityStripe::getThickStripe, param.getThickStripeStart());
            lambda.le(QualityStripe::getThickStripe, param.getThickStripeEnd());
        }

        if (param.getThinStripeStart() != null && param.getThinStripeEnd() != null) {
            lambda.ge(QualityStripe::getThinStripe, param.getThinStripeStart());
            lambda.le(QualityStripe::getThinStripe, param.getThinStripeEnd());
        }

        if (param.getLeptonemaStart() != null && param.getLeptonemaEnd() != null) {
            lambda.ge(QualityStripe::getLeptonema, param.getLeptonemaStart());
            lambda.le(QualityStripe::getLeptonema, param.getLeptonemaEnd());
        }

        if (param.getLightShadeLineStart() != null && param.getLightShadeLineEnd() != null) {
            lambda.ge(QualityStripe::getLightShadeLine, param.getLightShadeLineStart());
            lambda.le(QualityStripe::getLightShadeLine, param.getLightShadeLineEnd());
        }

        if (param.getVisibilityStart() != null && param.getVisibilityEnd() != null) {
            lambda.ge(QualityStripe::getVisibility, param.getVisibilityStart());
            lambda.le(QualityStripe::getVisibility, param.getVisibilityEnd());
        }

        if (param.getRecordStartTime() != null && param.getRecordEndTime() != null) {
            lambda.ge(QualityStripe::getRecordTime, param.getRecordStartTime());
            lambda.le(QualityStripe::getRecordTime, param.getRecordEndTime());
        }
        if (param.getTransectStartTime() != null && param.getTransectEndTime() != null) {
            lambda.ge(QualityStripe::getTransectTime, param.getTransectStartTime());
            lambda.le(QualityStripe::getTransectTime, param.getTransectEndTime());
        }
    }

}
